package down.xiaoheihe.work.Shiro;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
        @Bean
        public UserRealm userRealm() {
        return new UserRealm();
        }
        /**
         * 指定本系统sessionid, 问题: 与servlet容器名冲突, 如jetty, tomcat 等默认jsessionid,
         * 当跳出shiro servlet时如error-page容器会为jsessionid重新分配值导致登录会话丢失!
         *
         * @return
         */
        @Bean
        public SimpleCookie sessionIdCookie() {
                SimpleCookie simpleCookie = new SimpleCookie("shiro.session");
                // 防止xss攻击，窃取cookie内容
                simpleCookie.setHttpOnly(true);
                return simpleCookie;
        }

        /**
         * 会话管理
         * 默认使用容器session，这里改为自定义session
         * session的全局超时时间默认是30分钟
         *
         * @return
         */
        @Bean
        public DefaultWebSessionManager sessionManager() {
                DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
                // 会话超时时间，单位：毫秒
                sessionManager.setGlobalSessionTimeout(240 * 60 * 1000);
                // 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话
                sessionManager.setSessionValidationInterval(60 * 60 * 1000);
                // 是否开启定时清理失效会话
                sessionManager.setSessionValidationSchedulerEnabled(true);
                // 指定sessionid
                sessionManager.setSessionIdCookie(sessionIdCookie());
                // 是否允许将sessionId 放到 cookie 中
                sessionManager.setSessionIdCookieEnabled(true);
                // 是否允许将 sessionId 放到 Url 地址拦中
                sessionManager.setSessionIdUrlRewritingEnabled(false);
                return sessionManager;
        }

        @Bean
        public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm());
        return securityManager;
        }
        @Bean
        public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("http://localhost:9051/fail");
                Map<String, String> filterMap = new LinkedHashMap<>();
                filterMap.put("/select", "anon");
                filterMap.put("/toimages", "anon");
                filterMap.put("/toobjs", "anon");
                filterMap.put("/toitems", "anon");
                filterMap.put("/login", "anon");
                filterMap.put("/fail", "anon");
                filterMap.put("/fsyzm", "anon");
                filterMap.put("/getsalt", "anon");
                filterMap.put("/uploadALL", "anon");
                filterMap.put("/register", "anon");
                filterMap.put("/daorushuju", "anon");
                filterMap.put("/selectProducts", "anon");
                filterMap.put("/me", "authc");
                filterMap.put("/logout", "anon");
                filterMap.put("/liaotiantotal", "authc");
                filterMap.put("/orders", "authc");
                filterMap.put("/xiugaiziliao", "authc");
                shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
                return shiroFilterFactoryBean;
        }

/**
 * 开启Shiro注解模式，可以在Controller中的方法上添加注解
 * @param securityManager
 * @return
 */
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
        }
}